#include "snakeboardwidget.h"
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QString>
SnakeBoardWidget::SnakeBoardWidget(QWidget *parent) : QWidget(parent)
{
    init();
    show();
}

/**
 * 初始化
 * @brief SnakeBoardWidget::init
 */
void SnakeBoardWidget::init(){
    //设置颜色
    pe.setColor(QPalette::WindowText,Qt::green);
    //字体大小
    font.setPixelSize(40);

    int x = 20;
    int y = 20;
    int width = 100;
    int height = 40;
    int sepatator = 40;//间距
    //总分
    totalScoreLabel = new QLabel(this);
    totalScoreLabel->setText("总分:");
    totalScoreLabel->setGeometry(x, y, width, height);
    totalScoreLabel->setPalette(pe);
    totalScoreLabel->setFont(font);
    //总分值
    totalScoreLabelValue = new QLabel(this);
    totalScoreLabelValue->setText("0");
    totalScoreLabelValue->setGeometry(x + width + sepatator, y, width, height);
    totalScoreLabelValue->setPalette(pe);
    totalScoreLabelValue->setFont(font);

    //关卡
    currentLevelLabel = new QLabel(this);
    currentLevelLabel->setText("第 1 关");
    currentLevelLabel->setGeometry(x + 3 * sepatator, y + 2 * sepatator, width * 2, height);
    currentLevelLabel->setPalette(pe);
    currentLevelLabel->setFont(font);

    font.setPixelSize(20);
    pe.setColor(QPalette::WindowText,Qt::yellow);

    //通关分数
    passLevelScoreLabel = new QLabel(this);
    passLevelScoreLabel->setText("通关分数:");
    passLevelScoreLabel->setGeometry(x , y + 4 * sepatator, width * 2, height);
    passLevelScoreLabel->setPalette(pe);
    passLevelScoreLabel->setFont(font);
    //通关分数值
    passLevelScoreLabelValue = new QLabel(this);
    passLevelScoreLabelValue->setText("0");
    passLevelScoreLabelValue->setGeometry(x + width * 2, y + 4 * sepatator, width, height);
    passLevelScoreLabelValue->setPalette(pe);
    passLevelScoreLabelValue->setFont(font);

    //当前分数
    currentLevelScoreLabel = new QLabel(this);
    currentLevelScoreLabel->setText("得分:");
    currentLevelScoreLabel->setGeometry(x , y + 6 * sepatator, width * 2, height);
    currentLevelScoreLabel->setPalette(pe);
    currentLevelScoreLabel->setFont(font);
    //当前分数值
    currentLevelScoreLabelValue = new QLabel(this);
    currentLevelScoreLabelValue->setText("0");
    currentLevelScoreLabelValue->setGeometry(x + width * 2, y + 6 * sepatator, width, height);
    currentLevelScoreLabelValue->setPalette(pe);
    currentLevelScoreLabelValue->setFont(font);

}

/**
 *
 * 绘制
 * @brief SnakeBoardWidget::paintEvent
 */
void SnakeBoardWidget::paintEvent(QPaintEvent *){
    //绘制游戏背景
    QPainter painter(this);
    QBrush brush;
    QPen pen;
    brush.setStyle(Qt::BrushStyle::SolidPattern);
    brush.setColor(Qt::gray);
    pen.setColor(Qt::gray);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawRect(0, 0, width(), height()); //绘制矩形
}

/**
 * 设置关卡
 * @brief setLevel
 * @param level 关卡值
 */
void SnakeBoardWidget::setLevel(int level){
    QString s("第 ");
    s.append(QString::number(level, 10));
    s.append(" 关");
    currentLevelLabel->setText(s);
}

/**
 * 设置总分
 * @brief setTotalScore
 * @param totalScore 总分值
 */
void SnakeBoardWidget::setTotalScore(int totalScore){

    totalScoreLabelValue->setNum(totalScore);
}
/**
 * 设置通过关分数
 * @brief setPassScore
 * @param passScore
 */
void SnakeBoardWidget::setPassScore(int passScore){
    passLevelScoreLabelValue->setNum(passScore);
}

/**
 * 设置当前关 得分
 * @brief setCurrentScore
 * @param currentScore
 */
void SnakeBoardWidget::setCurrentScore(int currentScore){
    currentLevelScoreLabelValue->setNum(currentScore);
}
